<?php
//$url="http://pimg.39.net/PictureLib/A/CT1500016770/CT1500016777/2012-10-16/201210161408015788.jpg";
$url=preg_replace("/\s*http\:\/\/.*http\:\/\//isx","http://",urldecode($url));
$url=str_replace("BAIFENHAO","%",$url);
$haveReferer=1;
if(preg_match("/sinaimg\.cn/Uisx",$url)){
  $haveReferer=0;
}

if(preg_match("/39\.com/Uisx",$url)){
  $cookie="_wt_session_refer_cookie=http%253A%252F%252Fwww.39.net%252F; Hm_lpvt_ab2e5965345c61109c5e97c34de8026a=1350459594308; Hm_lpvt_1827105cf5cfac624c8dbf5159abfdb4=1350459576311; onlineId=4770284101207144433; area_info=CN450100|%D6%D0%B9%FA|%B9%E3%CE%F7|%C4%CF%C4%FE%CA%D0|%B5%E7%D0%C5; Hm_lvt_ab2e5965345c61109c5e97c34de8026a=1350459575708; Hm_lvt_1827105cf5cfac624c8dbf5159abfdb4=1350459576311";
}


require ("D:/longsuncollect/upload/source/class/collect/http.class2.php");
preg_match_all("/http\:\/\/([^\/]*)(\/[^<>\"\s]*)$/Uisx",$_GET[url],$nn);
$post=<<<TEXT
GET {$nn[2][0]} HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; Foxy/1; QQDownload 702; GTB7.2; Foxy/1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C)
Referer: http://{$nn[1][0]}/a/121017/4067349.html
Accept-Encoding: gzip, deflate
Host: {$nn[1][0]}
Connection: Keep-Alive


TEXT;

$nr=curl4($url,$cookie,$haveReferer);
echo $nr;


function curl3($post,$cookie="",$haveReferer=1){
$POSTFIELDS="";
    $haveReferer=($haveReferer=="")?1:$haveReferer;
    if(substr($post,0,7)=="http://"){
        $post=urltopoststr2($post,$cookie,$haveReferer);
    }

	preg_match_all("/^Host\:(\s*.*)$/Uimx",$post,$nn);
	$host=trim(chop($nn[1][0]));
	$site="http://".$host;
	preg_match_all("/^(POST|GET)\s+(.*)\s+HTTP/Umx",$post,$uu);
	$url=preg_replace("/\/+$/","",$site)."/".preg_replace("/^\/+/","",chop($uu[2][0]));
	preg_match_all("/^Referer\:(\s*.*)$/Uimx",$post,$nn);
	$Referer=trim(chop($nn[1][0]));
	preg_match_all("/(?:\r\n\r\n|\n\n)(.*)$/Uis",$post,$nn);
	$Querystring=trim(chop($nn[1][0]));
	preg_match_all("/boundary=(.*)$/Uim",$post,$nn);
	$boundary=trim(chop($nn[1][0]));
    $filename=$url;
	//$filename=$url.(preg_match("/\?/",$url)?"&":"?").$Querystring;
    //$filename=preg_replace("/http\:\/\/[^\/]*\//Uisx","/",$filename);
	$user_agent="Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)";
	$POSTFIELDS= $Querystring;

	$ch = curl_init();
    
    	
	$timeout = 60;
	curl_setopt ($ch, CURLOPT_URL, $filename);
	//curl_setopt ($ch, CURLOPT_MUTE, 0);
	curl_setopt ($ch, CURLOPT_VERBOSE, 0);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)");
	curl_setopt($ch, CURLOPT_HEADER, 1);//Ҫ�����ͷ��Ϣ
	if($uu[1][0]=="POST") curl_setopt($ch, CURLOPT_POST, 1);
	if(preg_match("/multipart/Uisx",$post)){
		curl_setopt($ch, CURLOPT_VERBOSE, 1);
		curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary")); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
	}
	if($Referer!="") curl_setopt($ch, CURLOPT_REFERER, $Referer);
	if($cookie!="") curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	if($Querystring!=""&&$uu[1][0]=="POST"){
		curl_setopt($ch, CURLOPT_POSTFIELDS, $Querystring);
	}
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$result = curl_exec($ch);
	curl_close($ch);

    preg_match("/(^.*)(?:\r\n\r\n|\n\n)(.*?)$/Uis",$result,$nn);
    $header=$nn[1];
    $body=$nn[2];
    
    if(@gzinflate(substr($body ,10))){
        $body= @gzinflate(substr($body ,10));
    }

	
	//---------------��chunk����

	if(strpos($result,'chunk')){
	    $tmpfile = tempnam('/tmp','webcache'); 
	    $fp = fopen($tmpfile,'w'); 
	    fwrite($fp,$result); 
	    fclose($fp); 
	    ob_start(); 
	    readgzfile($tmpfile); 
	    $result = ob_get_contents(); 
	    ob_end_clean(); 
	    unlink($tmpfile);
    }
    
    $result=$header."\r\n\r\n".$body;
	$nr=$result;
	//$nr=preg_replace("/<(\/?script[^>]*)>/Uisx","<!--\\1-->",$nr);
	return $nr;
}

function curl4($post,$cookie="",$haveReferer=1){
$POSTFIELDS="";

   $haveReferer=($haveReferer==="")?1:$haveReferer;
    if(substr($post,0,7)=="http://"){
        $post=urltopoststr2($post,$cookie,$haveReferer);
    }
	preg_match_all("/^Host\:(\s*.*)$/Uimx",$post,$nn);
	$host=trim(chop($nn[1][0]));
	$site="http://".$host;
	preg_match_all("/^(POST|GET)\s+(.*)\s+HTTP/Umx",$post,$uu);
	$url=preg_replace("/\/+$/","",$site)."/".preg_replace("/^\/+/","",chop($uu[2][0]));
	preg_match_all("/^Referer\:(\s*.*)$/Uimx",$post,$nn);
	$Referer=trim(chop($nn[1][0]));
	preg_match_all("/\r\n\r\n(.*)$/Uis",$post,$nn);
	$Querystring=trim(chop($nn[1][0]));
	preg_match_all("/boundary=(.*)$/Uim",$post,$nn);
	$boundary=trim(chop($nn[1][0]));
	$filename=$url;
	//$filename=$url.(preg_match("/\?/",$url)?"&":"?").$Querystring;
	$user_agent="Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)";
	$POSTFIELDS= $Querystring;

	$ch = curl_init();
	
	$timeout = 60;
	curl_setopt ($ch, CURLOPT_URL, $filename);
	//curl_setopt ($ch, CURLOPT_MUTE, 0);
	curl_setopt ($ch, CURLOPT_VERBOSE, 0);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)");
	curl_setopt($ch, CURLOPT_HEADER, 0);//Ҫ�����ͷ��Ϣ
	if($uu[1][0]=="POST") curl_setopt($ch, CURLOPT_POST, 1);
	if(preg_match("/multipart/Uisx",$post)){
		curl_setopt($ch, CURLOPT_VERBOSE, 1);
		curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary")); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
	}
	if($Referer!="") curl_setopt($ch, CURLOPT_REFERER, $Referer);
	if($cookie!="") curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	if($Querystring!=""&&$uu[1][0]=="POST"){
		curl_setopt($ch, CURLOPT_POSTFIELDS, $Querystring);
	}

	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	
	$result = @curl_exec($ch);
	curl_close($ch);

	preg_match_all("/(^.*)\r\n\r\n(.*?)$/Uis",$result,$nn);

	if(@gzinflate(substr($body ,10))){
		$result= @gzinflate(substr($nn[2][0] ,10));
	}
	//$result=$body;
	$nr=$result;
	//$cookie=getcookie($result);
	//setcookieto();
	//$nr=preg_replace("/<(\/?script[^>]*)>/Uisx","<!--\\1-->",$nr);
	return $nr;
}




function urltopoststr2($post,$cookies,$haveReferer=1){
$publicpoststr=<<<TEXT
GET {surl} HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, * / *
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; Foxy/1; QQDownload 702; GTB7.2; Foxy/1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C){Referer}
Accept-Encoding: gzip, deflate
cookies:{cookies}
Proxy-Connection: Keep-Alive
Host: {host}


TEXT;
        preg_match_all("/(http\:\/\/([^\/]*?))(.*)$/Uimx",$post,$mm);
        $host=$mm[2][0];
        $surl=$mm[3][0];
		if($haveReferer){
			$publicpoststr=str_replace("{Referer}","\r\nReferer:http://{$host}",$publicpoststr);
		}else{
		    $publicpoststr=str_replace("{Referer}","",$publicpoststr);
		}

        $publicpoststr=str_replace("{host}",$host,$publicpoststr);
        $publicpoststr=str_replace("{surl}",$surl,$publicpoststr);
        $publicpoststr=str_replace("{cookies}",$cookies,$publicpoststr);
		$publicpoststr=preg_replace("/\r\ncookies\:\s*$/m","",$publicpoststr);
        return trim($publicpoststr)."\r\n\r\n";
}

if(!function_exists('dquanurl')){
function dquanurl($nr,$wurl){
	$nr=preg_replace("/(src|location|<link[^>]*href|<EMBED[^>]*VALUE|action|background|imgurl|<PARAM\s+NAME\=movie\s+VALUE|href)\s*=\s*(\"|\')?([^\"\'\>\s]+)(\\2|\>|\s)/Uisxe",'"\\1=\"".quanurl("\\3",$wurl)."\""',$nr);
	$nr=preg_replace("/url\((\"|'|)?([^<>\s\"']+)\\1\)/Uisxe",'"url(".quanurl("\\2",$wurl).")"',$nr);
	return $nr;
}};

if(!function_exists('quanurl')){
function quanurl($subUrl,$wurl){
		$wurl=preg_replace("/\?.*$/Uisx","",$wurl);
		preg_match_all("/(http\:\/\/([^\/]*?))(.*)$/Uimx",$wurl,$mm);
		$host=$mm[2][0];
		 if(substr($subUrl,0,7)=="http://"){
		 	 $newurl=$subUrl;
		 }elseif(preg_match("/^\//",$subUrl)){
		 	 $newurl="http://".$host.$subUrl;
		 }elseif(preg_match("/^\.\.\//",$subUrl)){
			 $tmsuburl=$subUrl;
			 $tempparentUrl=$wurl;
			 preg_match_all("/(\.\.\/)/Uisx",$subUrl,$nn);
			 for($i=0;$i<count($nn[1]);$i++){
				 $tmsuburl=preg_replace("/^\.\.\//","",$tmsuburl);
				 $tempparentUrl=preg_replace("/(?!<\/)\/[^<>\/]*\/[^<>\/]*$/","/",$tempparentUrl);
			 }
			 $newurl=preg_replace("/\/$/","",$tempparentUrl)."/".$tmsuburl;
		 }else{
			 $newurl=preg_replace("/\/[^\/]*$/","/",$wurl).$subUrl;
		 }
		 return $newurl;
}};




?>